package com.itheima.并发工具类.Semaphore;

import java.util.concurrent.Semaphore;

public class MySemaphore {

    public static void main(String[] args) {
        //Semaphore使用场景:可以控制访问特定资源的线程数量
        Semaphore s = new Semaphore(2);
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                try {
                    s.acquire();
                    System.out.println("获得了通行证开始行驶");
                    Thread.sleep(2000);
                    System.out.println("归还通行证");
                    s.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
